package com.wwwarehouse.taskcenter.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.OwnBusinessUnitBean;

import java.util.List;


/**
 * @创建者 chenchao.
 * @创建时间 2017/9/24 19:52
 * @描述 ${TODO}
 */

public class OwnBusiUnitAdapter extends BaseAdapter  {

    private Context                   mContext;
    private List<OwnBusinessUnitBean> mCurrentList;
    private ListView                       mListView;
    public OwnBusiUnitAdapter(Context context, List<OwnBusinessUnitBean> list, ListView listView) {
        mContext = context;
        mCurrentList = list;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mCurrentList == null ? 0 : mCurrentList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCurrentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    ViewHolder viewHolder;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        OwnBusinessUnitBean ownBusinessUnitBean = mCurrentList.get(position);

        if (convertView == null) {
            convertView = parent.inflate(mContext, R.layout.own_busi_unit_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivGood = (ImageView) convertView.findViewById(R.id.iv_good);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                int itemHeight = (mListView.getMeasuredHeight() - mListView.getPaddingTop() - mListView.getPaddingBottom()) / 7;
                params.height = itemHeight;
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        BaseApplication.getApplicationInstance().displayImg(ownBusinessUnitBean.getBuIcon(), viewHolder.ivGood);
        viewHolder.tvName.setText(ownBusinessUnitBean.getBuName());
        return convertView;
    }


    static class ViewHolder{
        private ImageView ivGood;
        private TextView tvName;
    }
}
